// components/controlBar/controlBar.js

// import Area
const cloud = require('../../utils/cloudFunction')
const con = require('../../utils/control')

Component({
    /**
     * Component properties
     */
    properties: {

        roomMac: { // 设备所在区域mac
            type: Number,
            value: 0
        },
        roomId: {
            type: String,
            value: "roomId"
        },
        roomStatus: {
            type: Number,
            value: 50
        },
    },

    /**
     * Component initial data
     */
    data: {
        cl: '',
        bar: {
            // 禁用下的样式颜色 style
            background: "#363636", // 标签背景色
            switch: "#000000", // 开关背景色
            sliderActiveColor: "#777777", // 滑块左边颜色
            sliderBlockColor: "#898989", // 滑块左边颜色
            sliderBackgroundColor: "#4E4E4E", // 滑块右边颜色
            sliderDisable: true, // 滑块状态 true 为滑块不可选择 sliderDisable = true means no work
        },
    },

    /**
     * 组件的内部生命周期，详见 开发文档 - 组件生命周期
     * https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/lifetimes.html
     */
    lifetimes: {
        // 渲染组件时，按外部传值更新组件样式
        attached() {
            this.data.cl = setInterval(() => {
                this.refreshUI()
            }, 500);
        },

        detached() {
            // 在组件实例被从页面节点树移除时执行
            clearInterval(this.data.cl);
        },
    },
    /**
     * Component methods
     */
    methods: {
        // 刷新ui
        refreshUI() {
            if (this.properties.roomStatus) {
                this.setData({
                    bar: {
                        // active style
                        background: "(left, #0678AC, #33C9EA)",
                        switch: "#2E8298",
                        sliderActiveColor: "#E9F2F1",
                        sliderBlockColor: "#FFFFFF",
                        sliderBackgroundColor: "#20758A",
                        sliderDisable: false,
                    }
                })
            } else {
                this.setData({
                    bar: {
                        // disable style
                        background: "#363636",
                        switch: "#494B4A",
                        sliderActiveColor: "#777777",
                        sliderBlockColor: "#898989",
                        sliderBackgroundColor: "#4E4E4E",
                        sliderDisable: true,
                    },
                })
            }
        },

        // 区域设备控制开关
        roomDevAllControl(e) {
            // console.log('switch类型开关当前状态-----', e.detail.value);
            if (e.detail.value == false) {
                cloud.roomControl(this.properties.roomId, null, 0)
                con.turnOff(this.properties.roomMac)
                this.setData({
                    roomStatus: 0
                })
            } else {
                cloud.roomControl(this.properties.roomId, null, 50)
                con.turnOn(this.properties.roomMac)
                this.setData({
                    roomStatus: 50
                })
            }
        },

        setRoomBrightness(e) {
            var value = e.detail.value
            this.properties.deviceBrightness = value
            setTimeout(() => {
                con.setBrightness(this.properties.roomMac, value)
            }, 500);
        },

        setRoomBrightnessDone(e) {
            var value = e.detail.value
            cloud.roomControl(this.properties.roomId, null, value)
        }
    }
})